package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.gold.GoldDto;
import com.java110.intf.acct.IGoldV1InnerServiceSMO;
import com.java110.intf.acct.IGoldWithdrawalV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.gold.GoldWithdrawalPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("deleteGoldWithHoldBmoImpl")
public class DeleteGoldWithHoldBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IGoldWithdrawalV1InnerServiceSMO goldWithdrawalV1InnerServiceSMOImpl;


    @Autowired
    private IGoldV1InnerServiceSMO goldV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "gwId", "gwId不能为空");
        Assert.hasKeyAndValue(reqJson, "goldId", "goldId不能为空");
        Assert.hasKeyAndValue(reqJson, "communityId", "小区不能为空");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        GoldDto goldDto = new GoldDto();
        goldDto.setGoldId(reqJson.getString("goldId"));
        goldDto.setCommunityId(reqJson.getString("communityId"));
        List<GoldDto> goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        Assert.listOnlyOne(goldDtos, "金币账户不存在");

        GoldWithdrawalPo goldWithdrawalPo = BeanConvertUtil.covertBean(reqJson, GoldWithdrawalPo.class);
        int flag = goldWithdrawalV1InnerServiceSMOImpl.deleteGoldWithdrawal(goldWithdrawalPo);

        if (flag < 1) {
            throw new CmdException("删除数据失败");
        }

        context.setResponseEntity(ResultVo.success());
    }
}
